package com.mojian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mojian.service.MessageService;
import com.mojian.entity.SysMessage;
import com.mojian.mapper.SysMessageMapper;
import com.mojian.utils.IpUtils;
import com.mojian.utils.PageUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MessageServiceImpl implements MessageService {

    private final SysMessageMapper messageMapper;

    @Override
    public Page<SysMessage> getMessageList() {
        return messageMapper.selectPage(PageUtils.getPage(), new LambdaQueryWrapper<SysMessage>()
                .orderByDesc(SysMessage::getCreateTime));
    }

    @Override
    public Boolean add(SysMessage sysMessage) {
        String ip = IpUtils.getIp();
        sysMessage.setIp(ip);
        sysMessage.setSource(IpUtils.getIp2region(ip));
        messageMapper.insert(sysMessage);
        return true;
    }
}
